#!/usr/bin/python
#-*- coding: utf-8 -*-

from random import choice
from ftplib import FTP
import re
import time

# Configuration
url = "ftp.wwpdb.org"
path_ls = "pub/pdb/data/structures/"
path_file = path_ls + "divided/pdb/"
filename_ls = "ls-lR"
local_name_ls = "clean_list"
number_pdb_files = 2000
dir_dest = "pdb/"

# Download LS-lR file
print "Connecting to " + url
ftp = FTP(url)
print "Login processing."
ftp.login()

print "Downloading list file"
ftp.retrbinary('RETR '+ path_ls+filename_ls, open(local_name_ls, 'wb').write)

ftp.close()

# Generate clean list
f = open(local_name_ls, 'r')
list_pdb = []
list_pdb_clean = []
i = 0

for l in f:
    if "../../divided/pdb" in l:
        list_pdb.append("/".join((l.split(" ")[-1:][0].strip().split('/')[-2:]))+"\n")
        i +=1

for p in range(number_pdb_files):
    item = choice(list_pdb)
    list_pdb_clean.append(item)
    list_pdb.remove(item)

f.close()

print "There is "+ str(i) + " files and " + str(number_pdb_files) + " files."


# Downloading pdb files

print "Downloading all pdb files"
i = 0


for p in list_pdb_clean:
    i += 1
    ftp = FTP(url)
    ftp.login()
    
    clean_name = p.split('/')[-1:]
    clean_name = clean_name[0].strip()[3:].split('.')[0] + ".gz"
    print str(i) + " -- " + url + "/" + path_file+p
    ftp.retrbinary('RETR '+ path_file+p, open(dir_dest+clean_name, 'wb').write)

    ftp.close()
    

